package org.example

import scala.io.Source

object scalaFunYun2 {
  def main(args: Array[String]): Unit = {
    //map映射，生成一个元素个数和类型都不变的新的类型或集合
    def addNum:Int => Int = (x:Int) => x * 2
    val list1 = List(100,200,300).map(addNum)
    //忽略参数类型
    val list2 = List(100,200,300).map(x => x * 2)
    //当参数只出现一次，参数也可以忽略
    val list3 = List(100,200,300).map(_*2)
    println(list3)
    //逆序输出
    val s ="nba"
    println(s.reverse)
    val wordList =List("java","scala","cba")
    println(wordList.map(_.reverse))
    println(wordList.reverse)
    val score =Array(("张三",Array(5.5,6.6,7.7,8.8,8.0)),("李四",Array(6.0,6.5,7.0,7.5,9.0)))
    score.map(x =>(x._1,x._2.max)).foreach(println)
    score.map(tp =>(tp._1,tp._2.map(t => if (t >=8.0) 1 else 0).sum)).foreach(println)
    score.map(x =>(x._1,x._2.map(x =>if(x <6.0)1 else 0).sum)).foreach(println)
    score.map(x =>(x._1,x._2.count(x=> x <6.0))).foreach(println)//简化
    score.map(x =>(x._1,x._2.sum /x._2.length)).foreach(println)
    //filter过滤 类型和元素不变
    val arr1 =Array(1,2,3,4,5,6)
    arr1.filter(t => if (t % 2==0) true else false).foreach(println)//遍历偶数
    arr1.filter(t => t % 2==0).foreach(println)//遍历偶数，简化
    arr1.filter(_ % 2==1).foreach(println)//遍历基数再简化
    //输出集合中的回文单词
    val arr2=Array("I","love","you")
    arr2.filter(word =>word.reverse ==word).foreach(println)
    //flatMap扁平映射
    val wordFlat =List("I am hero","you are smart","I love scala")
    //分词
    val worldSplit =wordFlat.map(_.split(""))
    //打散扁平化
    val wordFlatten =worldSplit.flatten
    println(wordFlatten)
    val flatmapList =wordFlat.flatMap(_.split(""))
    println(flatmapList)
    //groupBy分组
    println(flatmapList.groupBy(_.charAt(0)).toList)
    /*//将wordFlat中相同的单词进行分组，并输出每个单词出现的次数
    //1、分组
    val wordSam =flatmapList.groupBy(word => word)
    println(wordSam)
    //2、统计次数
    val countMap =wordSam.map(tp =>(tp._1,tp._2.length))
    println(countMap)
    //3、将哈希表转为列表
    val countList =countMap.toList
    println(countList)
    //4、排序
    val sorList=countList.sortWith(_._2>_._2)
    println(sorList)
    //5、取前2
    val takeList =countList.take(2)
    println(takeList)*/
    /*//读取word.txt文档，统计出词频率最高的前3个单词并输出 找出回文单词并输出
    val words=Source.fromFile("src/main/resources/word.txt")
    words.foreach(print)
    val wordSam = flatmapList.groupBy(word => word)
    println(wordSam)
    val countMap = wordSam.map(tp => (tp._1, tp._2.length))
    println(countMap)
    val countList = countMap.toList
    println(countList)
    val sorList = countList.sortWith(_._2 > _._2)
    println(sorList)
    val takeList = countList.take(3)
    println(takeList)*/
   /* // 提取单词
    val words = content.split("\\W+")

    // 1. 按单词分组
    val wordGroup = words.groupBy(word => word)
    println("Grouped words:")
    println(wordGroup)

    // 2. 统计每个单词的出现次数
    val countMap = wordGroup.map(tp => (tp._1, tp._2.length))
    println("Word counts:")
    println(countMap)

    // 3. 将Map转换为列表
    val countList = countMap.toList
    println("Count list:")
    println(countList)

    // 4. 按出现次数降序排序
    val sortedList = countList.sortWith(_._2 > _._2)
    println("Sorted list:")
    println(sortedList)

    // 5. 取出现次数最多的前3个单词
    val top3Words = sortedList.take(3)
    println("Top 3 words with highest frequency:")
    top3Words.foreach { case (word, freq) =>
      println(s"$word: $freq")
    }

    // 找出回文单词
    val palindromeWords = words.filter(word => word == word.reverse).distinct
    println("Palindrome words:")
    palindromeWords.foreach(println)
  }
}*/
  }

}
